﻿using SwenAssigment.Classes;
using SwenAssigment.DBClasses;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SwenAssigment
{
    public partial class DutyReport : System.Web.UI.Page
    {
        public override void VerifyRenderingInServerForm(Control control)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int start = System.DateTime.Now.Year - 5;
                int end = System.DateTime.Now.Year + 5;
                for (int i = start; i <= end; i++)
                {
                    DropDownList1.Items.Add(i.ToString());
                }
                ListBox2.Items.Clear();
                foreach (WeekDate s in GenDate.AllWeekAndDate(Convert.ToInt32(DropDownList1.SelectedValue)))
                {
                    ListItem aa = new ListItem(s.date[0].ToShortDateString() + "~" + s.date[s.date.Count - 1].ToShortDateString(), s.WeekNo.ToString());
                    ListBox2.Items.Add(aa);
                }
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = Duty.AllDuty(Convert.ToInt32(DropDownList1.SelectedValue));
            GridView1.DataBind();
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.IsOtherMonth)
            {
                e.Day.IsSelectable = false;
                e.Cell.Text = "";
            }
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            TextBox1.Text = Calendar1.SelectedDate.ToString("dd/M/yyyy");
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex > -1)
            {
                GridView1.DataSource = Duty.MonthDuty(Convert.ToInt32(ListBox1.SelectedValue), Convert.ToInt32(DropDownList1.SelectedValue));
                GridView1.DataBind();

            }
            else
            {
                Label1.Visible = true;
            }
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            if (ListBox2.SelectedIndex > -1)
            {
                GridView1.DataSource = Duty.WeekDuty(ListBox2.SelectedValue);
                GridView1.DataBind();
            }
            else
            {
                Label2.Visible = true;
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox2.Items.Clear();
            foreach (WeekDate s in GenDate.AllWeekAndDate(Convert.ToInt32(DropDownList1.SelectedValue)))
            {
                ListItem aa = new ListItem(s.date[0].ToShortDateString() + "~" + s.date[s.date.Count - 1].ToShortDateString(), s.date[0].ToShortDateString() + "~" + s.date[s.date.Count - 1].ToShortDateString());
                ListBox2.Items.Add(aa);
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {

            GridView1.DataSource = Duty.ADuty(TextBox1.Text);
            GridView1.DataBind();
        }

        protected void Button7_Click(object sender, EventArgs e)
        {

            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            GridView1.RenderControl(htmlWrite);
            String EncodedString = Server.HtmlEncode(stringWrite.ToString());

            PrintDocument p = new PrintDocument();


            p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
            {
                e1.Graphics.DrawString(EncodedString, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));

            };
            try
            {
                p.Print();
            }
            catch (Exception ex)
            {
                throw new Exception("Exception Occured While Printing", ex);
            }
        }


        protected void Button8_Click(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=Avukat.xls");
                Response.Charset = "";

                Response.ContentType = "application/vnd.xls";
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

                GridView1.RenderControl(htmlWrite);
                Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
                Response.Write(stringWrite.ToString());
                Response.End();
            }
        }
    }
}